var strReg = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$";
var reg = new RegExp(strReg);
$(function () {

    $('#updateUser').click(function () {
        layer.open({
            type: 2,
            title: '修改用户信息',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['400px', '650px'],
            content: base_url + '/user/toUpdateUser'
        });
    });

    $('#updateUserBtn').click(function () {
        var ii = layer.load();
        $.ajax({
            url: base_url + "/user/updateUser",
            type: "post",
            data: $('#updateUserForm').serialize(),
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                layer.close(ii);
                if ("Success" == data.code) {
                    layer.confirm("修改成功！", {
                        icon: 6,
                        title: "系统提示",
                        btn: ["确定"]
                    }, function (index) {
                        layer.close(index);
                        var index2 = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index2);
                        parent.window.location.href = base_url + "/user/toUser";
                    });
                } else {
                    layer.alert("修改失败，请联系管理员！", {icon: 5});
                    return;
                }

            }
        });

    });

    $('#updateUserBtnM').click(function () {
        var ii = layer.load();
        $.ajax({
            url: base_url + "/user/updateUser",
            type: "post",
            data: $('#updateUserFormM').serialize(),
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                layer.close(ii);
                if ("Success" == data.code) {
                    layer.confirm("修改成功！", {
                        icon: 6,
                        title: "系统提示",
                        btn: ["确定"]
                    }, function (index) {
                        layer.close(index);
                        var index2 = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index2);
                        parent.window.location.href = base_url + "/manage/toPerson";
                    });
                } else {
                    layer.alert("修改失败，请联系管理员！", {icon: 5});
                    return;
                }

            }
        });

    });

    $('#updatePwd').click(function () {
        layer.open({
            type: 2,
            title: '修改密码',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['400px', '500px'],
            content: base_url + '/user/toUpdatePwd'
        });
    });

    $('#updatePwdBtn').click(function () {
        $('#oldPwdDiv').removeClass("has-error");
        $('#newPwd1Div').removeClass("has-error");
        $('#newPwd2Div').removeClass("has-error");
        $('#oldPwdMsg').html("");
        $('#newPwd1Msg').html("");
        $('#newPwd2Msg').html("");

        var oldPwd = $('#oldPwd').val();
        var newPwd1 = $('#newPwd1').val();
        var newPwd2 = $('#newPwd2').val();

        if (null == oldPwd || oldPwd == "") {
            $('#oldPwdDiv').addClass("has-error");
            $('#oldPwdMsg').html("旧密码 不能为空！");
            return;
        }
        if (null == newPwd1 || newPwd1 == "") {
            $('#newPwd1Div').addClass("has-error");
            $('#newPwd1Msg').html("新密码不能为空！");
            return;
        }
        if (null == newPwd2 || newPwd2 == "") {
            $('#newPwd2Div').addClass("has-error");
            $('#newPwd2Msg').html("新密码不能为空！");
            return;
        }
        if (newPwd1 != newPwd2) {
            $('#newPwd1Div').addClass("has-error");
            $('#newPwd2Div').addClass("has-error");
            $('#newPwd1Msg').html("两次输入密码不一致！");
            $('#newPwd2Msg').html("两次输入密码不一致！");
            return;
        }
        var ii = layer.load();
        var result;
        $.ajax({
            url: base_url + "/user/updatePwd",
            type: "post",
            data: $('#updatePwdForm').serialize(),
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                result = data;
            }
        });

        layer.close(ii);
        if (result.code == "PasswordError") {
            $('#oldPwdDiv').addClass("has-error");
            $('#oldPwdMsg').html("密码错误");
            return;
        } else if (!result.code == "Success") {
            layer.alert("密码更新失败，请联系管理员！", {icon: 5});
            return;
        } else {
            layer.confirm("密码更新成功！", {
                icon: 6,
                title: "系统提示",
                btn: ["确定"]
            }, function (index) {
                layer.close(index);
                var index2 = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index2);
            });
        }

    });

});

function toAddUser() {
    layer.open({
        type: 2,
        title: '添加用户',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['400px', '650px'],
        content: base_url + '/manage/toAddUser'
    });
}

function addUser() {
    $('#usernamediv').removeClass("has-error");
    $('#namediv').removeClass("has-error");
    $('#phonediv').removeClass("has-error");
    $('#maildiv').removeClass("has-error");
    $('#usernameMsg').html("");
    $('#nameMsg').html("");
    $('#phoneMsg').html("");
    $('#mailMsg').html("");
    var username = $('#username').val();
    if (null == username || username.length == 0) {
        $('#usernamediv').addClass("has-error");
        $('#usernameMsg').html("用户名不能为空！");
        return;
    }
    var name = $('#name').val();
    if (null == name || name.length == 0) {
        $('#namediv').addClass("has-error");
        $('#nameMsg').html("姓名不能为空！");
        return;
    }
    var phone = $('#phone').val();
    if (null == phone || phone.length == 0) {
        $('#phonediv').addClass("has-error");
        $('#phoneMsg').html("手机号不能为空！");
        return;
    }
    var mail = $('#mail').val();
    if (!reg.test(mail)) {
        $('#maildiv').addClass("has-error");
        $('#mailMsg').html("请输入正确的邮箱地址！");
        return;
    }

    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/addUser",
        type: "post",
        async:false,//设置同步方式，非异步！
        cache:false,//严格禁止缓存！
        data: $('#registerForm').serialize(),
        success: function (data) {
            layer.close(ii);
            if ("Success" == data.code) {
                layer.confirm( "注册成功，登录密码请从邮件中获取" , {
                    icon: 6,
                    title: "系统提示" ,
                    btn: [ "确定" ]
                }, function(index){
                    layer.close(index);
                    var findex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(findex);
                    parent.window.location.href = base_url + "/manage/toPerson";
                });
            } else {
                layer.alert(data.msg, {icon: 5});
                return;
            }
        }
    });
}

function deleteUser(id, username) {
    var result;
    layer.confirm("确认注销", {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        $.ajax({
            url: base_url + "/manage/deleteUser",
            type: "post",
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            data: {"username": username},
            success: function (data) {
                result = data;
            }
        });
        if ("Success" == result.code) {
            layer.alert("注销成功！", {icon: 6});
            $("#" + id).remove();
        } else {
            layer.alert("注销失败：" + result.msg, {icon: 5});
        }
    });
}

function updateUser(username) {
    layer.open({
        type: 2,
        title: '修改用户信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['400px', '650px'],
        content: base_url + '/manage/toUpdateUser?username=' + username
    });
}

function resetUser(username) {
    layer.confirm("确认重置密码", {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        $.ajax({
            url: base_url + "/manage/resetUser?username=" + username,
            type: "get",
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                if ("Success" == data.code) {
                    layer.alert("重置成功！", {icon: 6});
                } else {
                    layer.alert("重置失败：" + data.msg, {icon: 5});
                }
            }
        });
    });
}

function changeDepName() {
    var depId = $('#depId').val();
    if (null != depId && depId != "") {
        var depName = $('#depId').find("option:selected").text();
        $('#depName').val(depName);
    }
}



